<?php
/* Home controller 
** thaodx@gmail.com
*/
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends CI_Controller {
	public $layout_view = 'layout/ajax';
	// get facebook comment, like from URL
	public function fb()
	{
		$url = base64_decode($_POST['url']);
		if(strstr($url, ',')){
			$url = explode(',', $url);
		}
		$this->load->helper('social');
		//array('http://www.haivl.com/photo/58799', 'http://www.haivl.com/photo/603154', 'http://www.haivl.com/photo/604056')
		$fb = getFacebookCount($url);
        echo json_encode($fb);
	}
	public function topfbcmt()
	{
		$url = $_POST['url'];
		$this->load->helper('social');
		
		$fb = getTopFacebookComment($url, false);
        echo json_encode($fb);
        die;
	}
	public function loadmore()
	{
		$offset = intval($_POST['offset']);
		$cid = intval($_POST['cid']);

		$this->load->model('Post_model', 'homeModel');
		$this->load->helper(array('datetime', 'permalink', 'social', 'image'));
		if($cid > 0){
			$latestPost = $this->homeModel->getPostByCate($cid, $offset, CATE_LIMIT);
	       	$data = array('data' => $latestPost);
		}
		else{
			$latestPost = $this->homeModel->getLatestPost($offset, HOME_LIMIT);
	       	$data = array('data' => $latestPost);
        }
        if(!empty($data)){
			$html = $this->layout->view('ajax_loadmore', $data, true);
		}
		else{
			$html = '';
		}
		$lastItem = end($latestPost);
		$result = array(
			'html' => $html,
			'lastid' => intval($lastItem->id),
			'nextpage' => base64_encode($lastItem->id.'-'.$offset)
			);

		die(json_encode($result));
	}
}